import { createRouter, createWebHistory } from 'vue-router'
import home from '../pages/home/Index.vue'
import info from '../pages/home/rong/info/Index.vue'

const routes = [
  {
    path: '/',
    redirect: '/home',
  },
  // 1
  {
    path: '/home',
    // redirect: '/home/diancai/dianping',
    component: () => import('../pages/home/Index.vue'),
    redirect: '/home/index',
    meta: {
      isRequireLogin: true,
      title: '首页',
    },
    children: [
      //首页
      {
        path: '/home/index',
        component: () => import('../pages/home/index/Index.vue'),
        // meta: {
        //   title: '首页',
        // },
      },
      {
        path: '/home/diancai/dianping',
        component: () => import('../pages/home/diancai/dianping/Index.vue'),
        redirect: '/home/diancai/dianping/caip',
        meta: {
          title: '大众点评',
        },
        children: [
          {
            path: 'caip',
            component: () => import('../pages/home/diancai/dianping/caip/Index.vue'),
            meta: {
              title: '菜品',
            },
          },
          {
            path: 'yinp',
            component: () => import('../pages/home/diancai/dianping/yinp/Index.vue'),
            meta: {
              title: '饮品',
            },
          },
          {
            path: 'kaor',
            component: () => import('../pages/home/diancai/dianping/kaor/Index.vue'),
            meta: {
              title: '烤肉',
            },
          },
          {
            path: 'tianp',
            component: () => import('../pages/home/diancai/dianping/tianp/Index.vue'),
            meta: {
              title: '甜品',
            },
          },
        ],
      },

      //动态路由写死
      {
        path: '/home/rong/info',
        component: () => import('../pages/home/rong/info/Index.vue'),
        meta: {
          title: '账号信息',
        },
      },
      {
        path: '/home/diancai/tangshi',
        component: () => import('../pages/home/diancai/tangshi/Index.vue'),
        meta: {
          title: '堂食',
        },
      },
      {
        path: '/home/diancai/waimai',
        component: () => import('../pages/home/diancai/waimai/Index.vue'),
        meta: {
          title: '外卖',
        },
      },
      {
        path: '/home/shuju/zhuangkuang',
        component: () => import('../pages/home/shuju/zhuangkuang/Index.vue'),
        meta: {
          title: '实时经营概况',
        },
      },
      {
        path: '/home/shuju/fangshi',
        component: () => import('../pages/home/shuju/fangshi/Index.vue'),
        meta: {
          title: '消费方式',
        },
      },
      {
        path: '/home/shuju/num',
        component: () => import('../pages/home/shuju/num/Index.vue'),
        meta: {
          title: '销售量',
        },
      },
      {
        path: '/home/xiaoshou/luru',
        component: () => import('../pages/home/xiaoshou/luru/Index.vue'),
        meta: {
          title: '商品录入',
        },
      },
      {
        path: '/home/xiaoshou/shouyin',
        component: () => import('../pages/home/xiaoshou/shouyin/Index.vue'),
        meta: {
          title: '日常收银管理系统',
        },
      },
      {
        path: '/home/renyuan/huiyuan',
        component: () => import('../pages/home/renyuan/huiyuan/Index.vue'),
        meta: {
          title: '查看会员',
        },
      },
      {
        path: '/home/renyuan/yuangong',
        component: () => import('../pages/home/renyuan/yuangong/Index.vue'),
        meta: {
          title: '新增会员',
        },
      },
      {
        path: '/home/houqin/kuguan',
        component: () => import('../pages/home/houqin/kuguan/Index.vue'),
        meta: {
          title: '库管库存',
        },
      },
      {
        path: '/home/houqin/kucun',
        meta: {
          title: '库存盘点',
        },
        component: () => import('../pages/home/houqin/kucun/Index.vue'),
        children: [
          {
            path: '/home/houqin/kucun',
            redirect: '/home/houqin/kucun/xiangq',
          },
          {
            path: 'pand',
            meta: {
              title: '盘点',
            },
            component: () => import('../pages/home/houqin/kucun/pand/Index.vue'),
          },
          {
            path: 'xiangq',
            meta: {
              title: '详情',
            },
            component: () => import('../pages/home/houqin/kucun/xiangq/Index.vue'),
          },
        ],
      },
    ],
  },

  {
    path: '/login',
    component: () => import('../pages/login/Index.vue'),
    children: [],
  },
  {
    path: '/:pathMatch(.*)*',
    component: () => import('../pages/notFound/Index.vue'),
  },
]

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes,
})

//路由守卫
router.beforeEach((to, from, next) => {
  if (to.meta.isRequireLogin) {
    if (localStorage.getItem('user')) {
      // console.log(to.path)
      next()
      // next({ ...to.path })
      // next({ ...to.path, replace: true })
    } else {
      window.alert('请登录账号')
      next('/login')
    }
  } else {
    next()
  }

  // const user = localStorage.getItem('user')
  // if (user) {
  //   if (to.path === '/login') {
  //     next({ path: '/' })
  //   }
  //   if (to.name != null) {
  //     next()
  //   } else {
  //     router.addRoute({
  //       name: 'home',
  //       path: '/home',
  //       component: home,
  //       children: [{ path: '/home/rong/info', component: info }],
  //     })
  //     next({ ...to, replace: true })
  //   }
  // } else {
  //   if (to.path !== '/logon') {
  //     next({ path: '/logon' })
  //   } else {
  //     next()
  //   }
  // }
})

export default router
